// pages/employeeManage/employeeManage.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /*  */
    userInfo_share:{},
    /* tabContent数据 */
    contentIndex:'0',  //内容标记
    tabContentData:[
      {
        name:'员工列表',
        id:'0',
        emptyWord:'暂无员工数据！',
        showListEmpty:false,
        data:[],   //列表数据
        reqFnName:'getCorpUserInfoByName',  //获取列表数据方法名
        page:1,  //页码
        pageSize:10, //每页条数
      }
    ],
    /*  */
    employeeArgs:{
      name:"",  //员工姓名
      page:1,   //页码
      pageSize:10 //每页条数
    }
  },
  /*  */
  backEmployee(){
    wx.navigateBack({
      complete: (res) => {},
    })
  },
  /* 跳转至员工详情 */
  goToEmployeeDetail(e){
    let { info } = e.currentTarget.dataset;
    //
    console.log(info);
    app.globalData.fromEmployeeToEmployeeDetail = true;
    wx.navigateTo({
      url: '/pages/employeeDetail/employeeDetail',
      success:res=>{
        res.eventChannel.emit("fromEmployeeToEmployeeDetail",{
          info:info
        })
      }
    })
  },
  /* 输入员工姓名搜索 */
  insertEmployeeName:app.globalData.debounce(function(e){
    console.log(e.detail.value);
    this.setData({
      "employeeArgs.name":e.detail.value
    });
    //
    this.pageContentRefresh();
  },200),
  /* 获取员工数据 */
  getCorpUserInfoByName(){
    //
    wx.showLoading({
      title:'获取员工数据',
      mask:true
    });
    //
    this.setData({
      'employeeArgs.page':this.data.tabContentData[this.data.contentIndex*1].page
    });
    //
    return app.globalData.$http.call(this,{
      api:"/getCorpUserInfoByName",
      data:this.data.employeeArgs
    }).then(res=>{
      console.log(res)
      wx.hideLoading();
      if(res.data.code=='1'){
        if(res.data.body.length){
          let tmpData = this.data.tabContentData[this.data.contentIndex*1].data;
          let page = this.data.tabContentData[this.data.contentIndex*1].page;
          res.data.body.forEach(item=>{
            tmpData.push(item);
          });
          page++;
          //
          this.setData({
            [`tabContentData[${this.data.contentIndex*1}].page`]:page,
            [`tabContentData[${this.data.contentIndex*1}].data`]:tmpData,
            [`tabContentData[${this.data.contentIndex*1}].showListEmpty`]:false
          });
        }
        //列表为空
        if(!res.data.body.length&&!this.data.tabContentData[this.data.contentIndex*1].data.length){
          //
          this.setData({
            [`tabContentData[${this.data.contentIndex*1}].showListEmpty`]:true
          });
        }
        return Promise.resolve()
      }else{
        wx.showToast({
          icon:'none',
          title:'获取客户列表数据失败'
        });
      }
    }).catch(err=>{
      console.log(err);
      wx.hideLoading();
      wx.showToast({
        icon:'none',
        title:'获取客户列表数据失败'
      });
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //获取用户信息
    this.setData({
      userInfo_share:app.globalData.get_userInfo()
    });
    //
    let eventChannel = this.getOpenerEventChannel()
    //
    if(app.globalData.fromEmployeeToEmployeeManage){
      app.globalData.fromEmployeeToEmployeeManage = false;
      eventChannel.on("fromEmployeeToEmployeeManage",data=>{
        console.log(data);
        wx.setNavigationBarTitle({
          title: `${data.name}`,
        })
      });
    }
    //打开页面--默认获取客户列表数据
    if(!this.data.tabContentData[this.data.contentIndex*1].data.length){
      typeof this[this.data.tabContentData[this.data.contentIndex*1].reqFnName]==='function'&&this[this.data.tabContentData[this.data.contentIndex*1].reqFnName]();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /* 下拉刷新 */
  pageContentRefresh:function(){
    //
    this.setData({
      [`tabContentData[${this.data.contentIndex*1}].page`]:1,
      [`tabContentData[${this.data.contentIndex*1}].data`]:[],
    });
    //
    this[this.data.tabContentData[this.data.contentIndex*1].reqFnName]().then(()=>{
      wx.stopPullDownRefresh({
        complete: (res) => {},
      });
    }).catch(()=>{
      wx.stopPullDownRefresh({
        complete: (res) => {},
      });
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.pageContentRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this[this.data.tabContentData[this.data.contentIndex*1].reqFnName]();
  }
})